﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO.Custom;
using Business;
using Entity;
using FreshCms.BackEnd.ControlsCommon;
using Utilities;

namespace FreshCms.BackEnd.Controls.Systems.User
{
    public partial class UserManager : System.Web.UI.UserControl
    {
        #region check login and permission
        InPms chkPms = new InPms();
        public Int32 panelId = 0;
        private void checkLogin()
        {
            // Check and load UC
            if (Request.QueryString["ui"] != null)
                Int32.TryParse(Request.QueryString["ui"], out panelId);
            // check Pms
            if (panelId > 0)
                chkPms = InPmsBusiness.CheckSessionPms(panelId);
            if (chkPms == null)
                Response.Redirect("/BackEnd/xlogin.aspx?redirect=" + Utils.GetCurrentCmsPage().Replace("?", "@").Replace("&", "|"));
            // show Pms in Top Bar
            TopBarPms ucTopBar = Page.Master.FindControl("TopBarPms") as TopBarPms;
            ucTopBar.BindData(chkPms);
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            if (!Page.IsPostBack)
            {
                if (chkPms.PmsView == 1)
                {
                    BindStatus();
                    BindGroup();
                    BindData();
                }
            }
        }

        private void BindStatus()
        {
            ddlStatus.DataSource = StatusBusiness.GetShortListStatus();
            ddlStatus.DataTextField = "Value";
            ddlStatus.DataValueField = "Id";
            ddlStatus.DataBind();
            ddlStatus.Items[0].Attributes.Add("style", Constant.StyleDropAll);
            ddlStatus.Items[1].Attributes.Add("style", Constant.StyleDropDisable);
            ddlStatus.Items[2].Attributes.Add("style", Constant.StyleDropEnable);
        }

        private void BindGroup()
        {
            ddlGroup.DataSource = InGroupBusiness.GetInGroupList();
            ddlGroup.DataTextField = "GroupName";
            ddlGroup.DataValueField = "GroupId";
            ddlGroup.DataBind();
            ddlGroup.Items.Insert(0, new ListItem("-- Tất cả --", "0"));
        }

        public string mail, iden, phone, fullname = string.Empty;
        public Int32 groupid, stt = 0;
        private void BindData()
        {
            mail = Utils.GetQuery("mail", "");
            iden = Utils.GetQuery("iden", "");
            phone = Utils.GetQuery("phone", "");
            fullname = Utils.GetQuery("fullname", "");
            groupid = Utils.GetQuery("groupid", 0);
            stt = Utils.GetQuery("stt", -3);

            // bind stt to GUI
            ddlGroup.SelectedValue = groupid.ToString();
            ddlStatus.SelectedValue = stt.ToString();

            InUser obj = new InUser
                             {
                                 UserMail = mail,
                                 UserIdentity = iden,
                                 UserPhone = phone,
                                 UserFullname = fullname,
                                 GroupId = groupid,
                                 UserStatus = stt
                             };

            var lst = InUserBusiness.GetInUserListSearch(obj);
            if (lst.Count > 0)
            {
                rpt.DataSource = lst;
                rpt.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
                rpt.DataBind();
            }
        }

        void rpt_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                try
                {
                    Label lblStatus = (Label)e.Item.FindControl("lblStatus");
                    InUser obj = (InUser)e.Item.DataItem;

                    lblStatus.Text = StatusBusiness.GetShortStatusName(obj.UserStatus);

                    if (obj.UserStatus == 1)
                        lblStatus.CssClass = "label label-success";
                    else
                        lblStatus.CssClass = "label label-danger";
                }
                catch (Exception ex) { }

            }
        }
    }
}